package com.app.utils;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.fun.my.bean.BeanMyArticle;

public class MyNetUtil implements AsyncRequest {

	//sample request
	private static final String REQUEST_TEST = "request_test";
	
	//这个类处理回调
	private static final String REQUEST_SHARE = "request_share";

	//外部类处理回调
	public static final String REQUEST_ADD_COLLECT = "request_add_collect";
	public static final String REQUEST_CANCEL_COLLECT = "request_cancel_collect";
	public static final String REQUEST_ADD_HELP = "request_add_help";
	public static final String REQUEST_MY_INFO = "request_my_info";
	public static final String REQUEST_SIGN_IN = "request_sign_in";

	private static MyNetUtil mInstance = null;
	
	private BaseFragmentActivity mBaseFragmentActivity = null;

	private MyNetUtil() {

	}

	public static MyNetUtil getInstance() {
		if (mInstance == null) {
			mInstance = new MyNetUtil();
		}
		return mInstance;
	}

	// ================================================================================

	/***
	 * @param baseFragmentActivity
	 * @param ObjectType
	 *            1-文章，2-活动，3-抽奖,4-app
	 * @param ObjectID
	 */
	public void requestShare(BaseFragmentActivity baseFragmentActivity,
			int ObjectType, String ObjectID) {
		if (!LoginUtil.checkIsLogin()) {
			return;
		}

		mBaseFragmentActivity = baseFragmentActivity;

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();

		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());

			params.put("ObjectType", ObjectType);
			params.put("ObjectID", ObjectID);
		} catch (Exception e) {
		}
		map.put("ht", params);

		ApiClient.http_post_main(Setting.getShareSuccessUrl(), map, null, this,
				REQUEST_SHARE, true);
	}

	/**
	 * 
	 * @param baseFragmentActivity
	 * @param asyncRequest
	 * @param requestFlag
	 * @param beanMyArticle
	 * @param collectType
	 *            1：文章 2：视频 3：帖子;
	 */
	public void requestAddCollect(BaseFragmentActivity baseFragmentActivity,
			AsyncRequest asyncRequest, String requestFlag,
			BeanMyArticle beanMyArticle, int collectType) {

		boolean flag = LoginUtil.jumpLogin(baseFragmentActivity);
		if (!flag) {

			mBaseFragmentActivity = baseFragmentActivity;

			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject params = new JSONObject();
			try {
				params.put("DeviceID", Setting.DEVICEID);
				params.put("ApplicationID", Setting.APPLICATIONID);

				params.put("UserID", LoginUtil.getUserId());
				params.put("LoginToken", LoginUtil.getLoginToken());

				params.put("Tag", beanMyArticle.ArticleTag);
				params.put("Cover", beanMyArticle.Cover);
				
				params.put("TargetType", collectType);
				params.put("TargetID", beanMyArticle.ArticleID);
				params.put("Title", beanMyArticle.ArticleTitle);
				params.put("Abstract", beanMyArticle.ArticleDescription);

			} catch (Exception e) {
			}
			map.put("ht", params);

			ApiClient.http_post_main(Setting.getUserCollectAddUrl(), map, null,
					asyncRequest, REQUEST_ADD_COLLECT, true);
		}
	}

	/**
	 * 
	 * @param baseFragmentActivity
	 * @param asyncRequest
	 * @param TargetType
	 *            1：文章 2：视频 3：帖子;
	 * @param TargetID
	 */
	public void requestCancelCollect(BaseFragmentActivity baseFragmentActivity,
			AsyncRequest asyncRequest, String requestFlag, int TargetType, String TargetID) {

		boolean flag = LoginUtil.jumpLogin(baseFragmentActivity);
		if (!flag) {

			mBaseFragmentActivity = baseFragmentActivity;

			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject params = new JSONObject();
			try {
				params.put("DeviceID", Setting.DEVICEID);
				params.put("ApplicationID", Setting.APPLICATIONID);

				params.put("UserID", LoginUtil.getUserId());
				params.put("LoginToken", LoginUtil.getLoginToken());

				params.put("TargetType", TargetType);
				params.put("TargetID", TargetID);

			} catch (Exception e) {
			}
			map.put("ht", params);

			ApiClient.http_post_main(Setting.getUserCollectCancelUrl(), map,
					null, asyncRequest, REQUEST_CANCEL_COLLECT, true);
		}
	}
	
	/**
	 * 
	 * @param baseFragmentActivity
	 * @param asyncRequest
	 * @param requestFlag
	 * @param ArticleID
	 * @param ActionType	1：只增阅读数 2：增加点赞数和阅读数
	 */
	public void requestAddHelp(BaseFragmentActivity baseFragmentActivity,
			AsyncRequest asyncRequest, String requestFlag, String ArticleID, int ActionType) {

		boolean flag = LoginUtil.jumpLogin(baseFragmentActivity);
		if (!flag) {

			mBaseFragmentActivity = baseFragmentActivity;

			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject params = new JSONObject();
			try {
				params.put("DeviceID", Setting.DEVICEID);
				params.put("ApplicationID", Setting.APPLICATIONID);

				params.put("UserID", LoginUtil.getUserId());
				params.put("LoginToken", LoginUtil.getLoginToken());

				params.put("ArticleID", ArticleID);
				params.put("ActionType", ActionType);

			} catch (Exception e) {
			}
			map.put("ht", params);

			ApiClient.http_post_main(Setting.getUserArticleHelpAddUrl(), map, null, asyncRequest, REQUEST_ADD_HELP, true);
		}
	}

	/**
	 * 获取我的个人信息，收藏数量等
	 */
	public void requestMyInfo(BaseFragmentActivity baseFragmentActivity,AsyncRequest asyncRequest) {

		if (!LoginUtil.checkIsLogin()) {
			return;
		}

		mBaseFragmentActivity = baseFragmentActivity;

		String url = Setting.getMyUserInfoUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
		} catch (Exception e) {
		}
		map.put("ht", params);
		ApiClient.http_post_main(url, map, null, asyncRequest, REQUEST_MY_INFO, true);
	}

	/**
	 * 请求签到
	 */
	public void requestSignIn(BaseFragmentActivity baseFragmentActivity,AsyncRequest asyncRequest) {
		boolean flag = LoginUtil.jumpLogin(baseFragmentActivity);
		if (flag) {
			return;
		}

		mBaseFragmentActivity = baseFragmentActivity;

		String url = Setting.getMySignInUrl();
		mBaseFragmentActivity.showProgressDialog();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
		} catch (Exception e) {
		}
		map.put("ht", params);
		ApiClient.http_post_main(url, map, null, asyncRequest, REQUEST_SIGN_IN, true);
	}

	// ================================================================================
	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_TEST)) {
			LogUtilBase.LogD(null, "REQUEST_TEST success");
			mBaseFragmentActivity.hideProgressDialog();
		} else if (object.equals(REQUEST_SHARE)) {
			LogUtilBase.LogD(null, "REQUEST_SHARE success");

		}
	}

	@Override
	public void RequestError(Object object, int errorId, String data) {
		if (object.equals(REQUEST_TEST)) {
			LogUtilBase.LogD(null, "REQUEST_TEST fail");
			mBaseFragmentActivity.hideProgressDialog();
		} else if (object.equals(REQUEST_SHARE)) {
			LogUtilBase.LogD(null, "REQUEST_SHARE fail");
		}
	}

}
